<template>
  <el-form ref="form" :rules="rules" :model="currentNews" label-width="100px" class="demo-form">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card">
          <el-form-item label="标题" prop="title">
            <el-input v-model="currentNews.title"></el-input>
          </el-form-item>
          <el-form-item label="来源" prop="source" style="width: 300px;">
            <el-input v-model="currentNews.source"></el-input>
          </el-form-item>
          <el-form-item label="版式" prop="type">
            <el-select v-model="currentNews.type" placeholder="请选择">
              <el-option
                v-for="item in NewsTypes"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item >
          <el-form-item label="图片" prop="cover1" v-if="currentNews.type===1">
            <image-uploader v-model="currentNews.cover1" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>
          </el-form-item>
          <el-form-item label="视频" prop="video" v-if="currentNews.type===2" style="width: 300px;">
            <media-uploader v-model="currentNews.video"  accept="video" :max-size="51200" :width="540" :height="360"></media-uploader>
          </el-form-item>
          <el-row  v-if="currentNews.type===3">
            <el-col :span="4" style="min-width:300px;">
              <el-form-item label="图片1" prop="cover1"  >
                <image-uploader v-model="currentNews.cover1" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>
              </el-form-item>
            </el-col>
            <el-col :span="4" style="min-width:300px;">
              <el-form-item label="图片2" prop="cover2" >
                <image-uploader v-model="currentNews.cover2" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>
              </el-form-item>
            </el-col>
            <el-col :span="4" style="min-width:300px;">
              <el-form-item label="图片3" prop="cover3">
                <image-uploader v-model="currentNews.cover3" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="积分" prop="score" style="width: 300px;">
            <el-input-number type="number" v-model="currentNews.score" :min="0" :step="1"></el-input-number>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <!--<editor v-model="currentNews.content" height="300px"></editor>-->
               <u-editor v-model="currentNews.content" ></u-editor>
          </el-form-item>
          <el-form-item>
            <span style="float: right;">
               <el-button type="primary"  @click="save('form',0)">保存</el-button>
               <el-button type="primary" v-if="adminInfo.userRole==='ADMIN'" @click="save('form',1)">发布 </el-button>
               <el-button type="primary" v-if="adminInfo.userRole!=='ADMIN'" @click="save('form',3)">提交审核 </el-button>

            <el-button @click="back">返回</el-button>
               </span>
          </el-form-item>
        </el-card>

      </el-col>
      <!--<el-col :span="7">-->
        <!--<el-card class="box-card">-->
          <!--<div slot="header" class="clearfix">-->
            <!--<span>基本信息</span>-->
          <!--</div>-->
          <!--<el-form-item label="图片" prop="image">-->
            <!--<image-uploader v-model="currentNews.image" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>-->
          <!--</el-form-item>-->
          <!--<el-form-item label="封面图" prop="image">-->
            <!--<image-uploader v-model="currentNews.cover" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>-->
          <!--</el-form-item>-->
          <!--<el-form-item label="视频" prop="video">-->
            <!--<media-uploader v-model="currentNews.video"  accept="video" :max-size="51200" :width="90" :height="60"></media-uploader>-->
          <!--</el-form-item>-->
        <!--</el-card>-->

      <!--</el-col>-->
    </el-row>

  </el-form>
</template>
<script>
import { mapGetters } from "vuex";
import Editor from "@/components/editor";
import UEditor from "@/components/u-editor";
import ImageUploader from "@/components/image-uploader";
import MediaUploader from "@/components/media-uploader";
import Status from "@/libs/status";
import { checkNumber } from "@/libs/checkRules";
export default {
  props: {
    isClose: Boolean
  },
  created() {},
  mounted() {},
  computed: {
    ...mapGetters({
      adminInfo: "adminInfo",
      currentNews: "currentNews",
      uploadImgAttr: "uploadImgAttr"
    })
  },
  data() {
    return {
      NewsTypes: Status.NewsTypes,
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 3, max: 50, message: "长度在 3 到 50 个字符", trigger: "blur" }
        ],
        content: [{ required: false, message: "请输入内容", trigger: "blur" }],
        type: [{ required: true, message: "请选择版式", trigger: "blur" }],
        score: [
          { required: true, message: "请输入积分", trigger: "blur" },
          { validator: checkNumber, trigger: "blur" }
        ],
        cover1: [{ required: true, message: "请上传图片1", trigger: "blur" }],
        cover2: [{ required: true, message: "请上传图片2", trigger: "blur" }],
        cover3: [{ required: true, message: "请上传图片3", trigger: "blur" }],
        video: [{ required: true, message: "请上传视频", trigger: "blur" }]
      }
    };
  },
  methods: {
    save(formName, status) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.currentNews.status = status;
          this.$store
            .dispatch("addOrUpdateNewsS", this.currentNews)
            .then(ret => {
              this.$notify({
                title: "添加成功",
                type: "success"
              });
              this.back();
            });
        } else {
          return false;
        }
      });
    },
    back() {
      this.$emit("close");
    }
  },
  components: { Editor, ImageUploader, MediaUploader, UEditor }
};
</script>
<style scoped>
.editor_line_height {
  line-height: 14px;
}
</style>
